using System.Collections.Generic;
using Kopernikus.Collections.Generic;

namespace Kopernikus.Parsing.Utility
{
    public class SetMap<K, T>
    {
        private readonly Set<T> m_missingSet = new Set<T>();
        private readonly Dictionary<K, Set<T>> m_sets = new Dictionary<K, Set<T>>();

        public Set<T> GetSet(K key)
        {
            Set<T> set;
            return m_sets.TryGetValue(key, out set) ? set : m_missingSet;
        }

        public Set<T> MakeSet(K key)
        {
            Set<T> set;
            if (!m_sets.TryGetValue(key, out set))
            {
                m_sets.Add(key, set = new Set<T>());
            }
            return set;
        }
    }
}